package com.xike.microsysserver.controller;


import com.alibaba.fastjson.JSONObject;
import com.xike.entiy.ResultBean;
import com.xike.jwt.AuthResult;
import com.xike.microsysserver.entity.acl.User;
import com.xike.microsysserver.service.LoginService;
import com.xike.microsysserver.service.RoleService;
import com.xike.microsysserver.uitl.Constant;
import com.xike.microsysserver.uitl.JWTUtil;
import com.xike.microsysserver.uitl.MD5;
import com.xike.microsysserver.uitl.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Auther: wuson  <br/>
 * Date: 2021/3/10:15:49  <br/>
 * Description:
 */
@RefreshScope
@Api("平台端登陆")
@Slf4j
@RequestMapping("/admin/acl")
@RestController
public class LogInController {
    @Autowired(required = false)
    private LoginService loginService;
    @Autowired(required = false)
   private RabbitTemplate rabbitTemplate;
    @Resource
    private RoleService roleService;
    @ApiOperation("平台端用户登录")
    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity addOne(@RequestBody User currentuser){

        //查询账号
        User user = loginService.selectByAccount(currentuser.getUsername());
        String token = "";
        //验证账号密码
        if(user !=null&& user.getUsername().equals(currentuser.getUsername())){
            if(user.getPassword().equals(MD5.encrypt(currentuser.getPassword()))){
                //认证通过，产生JWT令牌
                Map<String, Object> claims = new HashMap<>();
                claims.put("memberName", user.getUsername());
                token = JWTUtil.createToken(claims);
                AuthResult authResult = AuthResult.builder().token(token).build();
                Map<String, Object> userToken = new HashMap<>();
                userToken.put("token",token);
                return ResponseEntity.ok().data(userToken);
            }
            //密码错误
            return ResponseEntity.error(Constant.WRONG_PASSWORD);
        }
        //无效账号
        return ResponseEntity.error(Constant.INVALID_ACCOUNT_NUMBER);
    }

    @ApiOperation("用户注册")
    @PostMapping(value = "/register",produces = MediaType.APPLICATION_JSON_VALUE)
    public String register(@RequestBody User user){
        //先向数据库中，添加数据
        log.info("接收到的注册信息：{}",user);
        int state = loginService.register(user);
        User currentuser = loginService.getOneByusername(user.getUsername());
        String[] roleId = new String[1];
        roleId[0]="1391320946563936258";
        roleService.saveUserRoleRealtionShip(currentuser.getId(), roleId);
//        //向RabbitMQ中生产数据
//        Map<String,Object> data = new HashMap<>();
//        data.put("msg",memberInfoEntity.getUser_name() + "注册成功");
//        data.put("email",memberInfoEntity.getEmail());
//        data.put("id", UUID.randomUUID().toString());//幂等性ID
        //交换机的名称，路由键，具体的数据
//        rabbitTemplate.convertAndSend("email_fanoutExchange",null,data);
        return JSONObject.toJSONString(ResultBean.builder().code(200).msg("注册成功").build());
    }
}
